<?php
/**
 * Wertet die Adresse Aus
 *
 */

/**
 * Dependencies
 *
 * @see Kanedo_Router_Request
 * @see Kanedo_Router_Exception
 */
require_once 'Kanedo/Router/Request.php';
require_once 'Kanedo/Router/Exception.php';

class Kanedo_Router
{
	/**
	 * Speichert den Request ($_SERVER['REQUEST_URI'];)
	 * @access protected
	 * @var string
	 */
	protected $_request = null;
	
	/**
	 * BaseUrl
	 *
	 * @var string
	 */
	protected $baseUrl;
	/**
	 * Initialisiert den Router 
	 *
	 */
	public function __construct()
	{
		$this->_request = $_SERVER['REQUEST_URI'];
	}
	
	/**
	 * Speichert die BaseUrl in $this->baseUrl
	 *
	 * @param string $base
	 */
	public function setBaseUrl($base)
	{
		$this->baseUrl = $base;
	}
	
		/**
		 * Interpretiert den Request
		 *
		 * @return Kanedo_Router_Request
		 */
		public function parseRequest()
		{

		 	if ((strpos($this->_request, $this->baseUrl) !== 0)) 
		 	{
			 	throw new Kanedo_Application_Controller_Exception('no_file', 404);	
			}
			$request = substr($this->_request, strlen($this->baseUrl));  
			$request = explode("/", $request);
			$request = preg_replace ("#(\?.*)#", '', $request);
			$requestObject = new Kanedo_Router_Request();
			
			$requestObject->setBaseUrl($this->baseUrl);
			
			if (isset($request[0]) && $request[0] !== null && $request[0] !== "" ) 
			{
				$requestObject->setGroup($request[0]);
			}
			if (isset($request[1]) && $request[1] !== null && $request[1] !== "") 
			{	
				$requestObject->setController($request[1]);
			}
			
			if (isset($request[2]) && $request[2] !== null && $request[2] !== "") 
			{
				$requestObject->setAction($request[2]);
			}
			
				foreach ($_GET as $key => $val)
				{
					$requestObject->setGetParam($key, $val);
				}
				
				foreach ($_POST as $key => $val)
				{
					$requestObject->setPostParam($key, $val);
				}
		
		$requestObject->setRequestedUri($_SERVER['REQUEST_URI']);
		return $requestObject;
			
		}
}
?>